package org.snail.web.config;

import org.quartz.SimpleTrigger;
import org.snail.chat.service.chatchain.quartz.DingGroupSynQuartzJob;
import org.snail.constant.QuartzConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
@EnableScheduling
public class QuartzElConfig {

    /**
     * 配置定时器quartz     ==========================================================================================================
     */
    // 配置定时任务
    @Bean(name = "jobDetail")
    public MethodInvokingJobDetailFactoryBean getMethodInvokingJobDetailFactoryBean(DingGroupSynQuartzJob dingGroupSynQuartzJob) {
        MethodInvokingJobDetailFactoryBean jobDetailFactory = new MethodInvokingJobDetailFactoryBean();
        jobDetailFactory.setGroup(QuartzConstant.JOB_GROUP_NAME);
        jobDetailFactory.setName(QuartzConstant.JOB_SYN_NAME);
        // 是否并发执行
        jobDetailFactory.setConcurrent(false);
        // 为需要执行的实体类对应的对象
        jobDetailFactory.setTargetObject(dingGroupSynQuartzJob);
        // 需要执行的方法
        jobDetailFactory.setTargetMethod("execute");
        return jobDetailFactory;
    }

    // 配置触发器
    @Bean(name = "simpleTrigger")
    public SimpleTriggerFactoryBean getSimpleTriggerFactoryBean() {
        SimpleTriggerFactoryBean simpleTriggerFactory = new SimpleTriggerFactoryBean();
        simpleTriggerFactory.setGroup(QuartzConstant.TRIGGER_GROUP_NAME);
        simpleTriggerFactory.setName(QuartzConstant.TRIGGER_SYN_NAME);
        //不重复执行
        simpleTriggerFactory.setRepeatCount(0);
        return simpleTriggerFactory;
    }

    // 配置Scheduler
    @Bean(name = "scheduler")
    public SchedulerFactoryBean getSchedulerFactoryBean(SimpleTrigger simpleTrigger) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        return bean;
    }

}
